package com.baihuabooks.scoresys.servlets;

import com.alibaba.fastjson.JSON;
import com.baihuabooks.scoresys.common.Config;
import com.baihuabooks.scoresys.common.DBUtil;
import com.baihuabooks.scoresys.common.Page;
import com.baihuabooks.scoresys.entity.Customer;
import com.baihuabooks.scoresys.entity.Response;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import sun.security.provider.MD5;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thinkpad on 2016/5/15 0015.
 */
public class UserServlet extends HttpServlet {



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Response response = new Response();

        if ("admin".equals(username)) {
            String targetPass = Config.get("user.password");
            String passwordMD5 = DigestUtils.md5Hex(password);
            if (targetPass.equals(passwordMD5)) {
                HttpSession session = req.getSession(true);
                session.setAttribute("user", "admin");
                response.setCode(0);
            } else {
                response.setCode(1);
            }
        } else {
            response.setCode(1);
        }

        resp.getWriter().print(response.toString());
    }
}
